//package com.example.demo.hotload.db;
//
//import java.io.File;
//
//import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.cfg.Configuration;
//import com.syj.domain.User;
//import org.springframework.beans.factory.support.BeanDefinitionBuilder;
//import org.springframework.beans.factory.support.DefaultListableBeanFactory;
//import org.springframework.context.ApplicationContext;
//import xyz.erupt.core.util.EruptSpringUtil;
//
///**
// * <p> * Title: 重新创建SessionFactory的测试类 * </p>
// */
//public class ResetHbmTest {
//    public static void main(String[] args) throws Exception {
//        HibernateConfiguration configuration = new HibernateConfiguration();
//        configuration.configure("/hibernate.cfg.xml");
//        SessionFactory sessionFactory = configuration.buildSessionFactory();
//        Session session = sessionFactory.openSession();
//        ApplicationContext ctx = EruptSpringUtil.getApplicationContext();
//        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)ctx.getAutowireCapableBeanFactory();
//        // 删除之前的sessionFactory
//        defaultListableBeanFactory.removeBeanDefinition("sessionFactory");
//        // 注入新的sessionFactory
//        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(SessionFactory.class);
//        defaultListableBeanFactory.registerBeanDefinition("sessionFactory",beanDefinitionBuilder.getBeanDefinition());
//
//
//        session.beginTransaction();
//        User u1 = new User();
//        session.save(u1);
//        session.getTransaction().commit();
//        session.refresh(u1);
//        System.out.println("user Id:" + u1.getId() + " name:" + u1.getName());
//        sessionFactory.close();
//
//        testReplaceXml();// 改变pojo xml文件
//        configuration.reset();// 重置
//        configuration.configure("/hibernate.cfg.xml");
//        sessionFactory = configuration.buildSessionFactory();
//        // buildSessionFactory
//        session = sessionFactory.openSession();
//        session.beginTransaction();
//        User u2 = new User();
//        session.save(u2);
//        session.getTransaction().commit();
//        session.refresh(u2);
//        System.out.println("user Id:" + u2.getId() + " name:" + u2.getName());
//    }
//
//
//    /**
//     * * Description:测试修改User.hbm.xml ，为了简单没考虑太多 简单替换
//     */
//    public static void testReplaceXml() throws Exception {
//        File file = new File(User.class.getResource("User.hbm.xml").getFile());
//        String userHbmXml = org.apache.commons.io.FileUtils.readFileToString(file, "UTF-8");
//        userHbmXml = userHbmXml.replaceAll("(select 1)", "(select 2)");
//        org.apache.commons.io.FileUtils.writeStringToFile(file, userHbmXml);
//    }
//}
//
//class HibernateConfiguration extends Configuration {
//    public HibernateConfiguration() {
//        super();
//    }
//
//    public void reset() {
//        super.reset();
//        // 关键在这里,这个方法是protected的通过继承转换为public
//    }
//}